<template>
  <div class="main-center">
    <el-card class="register-card">
      <h2>学生注册</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="90px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender">
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker v-model="form.birthDate" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="院校" prop="school">
          <el-input v-model="form.school" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" />
        </el-form-item>
        <el-form-item label="学历" prop="degree">
          <el-select v-model="form.degree">
            <el-option label="本科" value="本科" />
            <el-option label="硕士" value="硕士" />
            <el-option label="博士" value="博士" />
          </el-select>
        </el-form-item>
        <el-form-item label="毕业时间" prop="graduateDate">
          <el-date-picker v-model="form.graduateDate" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="项目经历" prop="projectExperience">
          <el-input v-model="form.projectExperience" type="textarea" />
        </el-form-item>
        <el-form-item label="获奖情况" prop="award">
          <el-input v-model="form.award" type="textarea" />
        </el-form-item>
        <el-form-item label="证书" prop="certificate">
          <el-input v-model="form.certificate" type="textarea" />
        </el-form-item>
        <el-form-item v-if="errorMsg">
          <div style="color: #f56c6c; font-size: 13px;">{{ errorMsg }}</div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onRegister" :loading="loading">注册</el-button>
          <el-button @click="goLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from '@/utils/axios'

const router = useRouter()
const formRef = ref()
const loading = ref(false)
const errorMsg = ref('')
const form = ref({
  username: '', password: '', name: '', gender: null, birthDate: '', phone: '', email: '', school: '', major: '', degree: '', graduateDate: '', projectExperience: '', award: '', certificate: ''
})
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  birthDate: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
  school: [{ required: true, message: '请输入院校', trigger: 'blur' }],
  major: [{ required: true, message: '请输入专业', trigger: 'blur' }],
  degree: [{ required: true, message: '请选择学历', trigger: 'change' }],
  graduateDate: [{ required: true, message: '请选择毕业时间', trigger: 'change' }]
}
const onRegister = () => {
  formRef.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    errorMsg.value = ''
    try {
      await axios.post('/register/student', form.value)
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } catch (e) {
      errorMsg.value = e.message || e?.response?.data?.message || '注册失败'
    }
    loading.value = false
  })
}
const goLogin = () => router.push('/login')
</script>

<style scoped>
.register-card {
  max-width: 500px;
  margin: 60px auto;
  padding: 30px 20px;
}
</style> 